<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="queryForm">
				<el-form-item>
					<el-input v-model="queryForm.keyword" placeholder="员工姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getEmployees">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.list" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="员工姓名" width="100" sortable>
			</el-table-column>
      <el-table-column prop="phone" label="电话" width="100" sortable>
      </el-table-column>
			<el-table-column prop="email" label="邮箱" min-width="100" sortable>
			</el-table-column>
      <el-table-column prop="age" label="年龄" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" sortable>
        <template scope="scope">
          <span v-if="scope.row.state=='0'" style="color: red">已停用</span>
          <span v-if="scope.row.state=='1'" style="color: green">已启用</span>
        </template>
      </el-table-column>
      <el-table-column prop="departmentName" label="部门" min-width="150" sortable>
      </el-table-column>
<!--      <el-table-column prop="logininfoId" label="登录信息ID" min-width="150" sortable>-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="shopId" label="店铺" min-width="150" sortable>-->
<!--      </el-table-column>-->
			<el-table-column label="操作" width="150" fixed="right">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          style="float: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryForm.currentPage"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="queryForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
		</el-col>
		<!--新增界面-->
		<el-dialog title="新增"  :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门管理" prop="manager">
          <el-select v-model="saveForm.manager" placeholder="请选择" value-key="id" clearable>
            <el-option
                v-for="item in employees"
                :key="item.id"
                :label="item.username"
                :value="item">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺" prop="name">
          <el-input v-model="saveForm.shop_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">开启</el-radio>
            <el-radio class="radio" :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	// import util from '../../common/js/util'
	// //import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	import department from "./Department.vue";

  export default {
    computed: {
      department() {
        return department
      }
    },
		data() {
			return {
        employees:[],
        shops:[],
        queryForm: {
					keyword: '',
          currentPage:1,
          pageSize:5
				},
				pageInfo:{
          list: [],
          total: 0,
        },
				listLoading: false,
				sels: [],//列表选中列

        saveFormVisible: false,//保存界面是否显示
        saveLoading: false,
        saveFormRules: {
					username: [
						{ required: true, message: '请输入员工姓名', trigger: 'blur' }
					]
				},
				//保存界面数据
				saveForm: {
          username: '',
          manager:{
            id:null,
            username:""
          },
          state:null,
          shop:{
            id:null,
            name:""
          }
				}
			}
		},
		methods: {
      //查询管理员数据
      getManager() {
        this.$http.get('/employee',)
            .then(res => res.data)
            .then(res => {
              if (res.success) {
                this.employees = res.data;
              } else {
                this.$message({
                  message: res.message,
                  type: 'error'
                });
              }
            })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val;
        this.getEmployees();
      },
      handleCurrentChange(val) {
        this.queryForm.currentPage = val;
        this.getEmployees();
      },
      //获取部门列表
      getEmployees() {
        // this.listLoading = true;
        // NProgress.start();
        this.$http.post('/employee/query', this.queryForm)
            .then(res => res.data)
            .then(res => {
              if (res.success) {
                this.pageInfo.list = res.data.list;
                this.pageInfo.total = res.data.total;
              } else {
                this.$message({
                  message: res.message,
                  type: 'error'
                });
              }
            })
            .catch(error => {
              console.log(error)
            })
      },
      //删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          this.$http.delete('/employee/' + row.id)
              .then(res => res.data)
              .then(res => {
                if (res.success) {
                  this.listLoading = false;
                  this.handleCurrentChange(1);
                  this.$message({
                    message: "删除成功！",
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: res.message,
                    type: 'error'
                  });
                }
              })
              .catch(error => {
                console.log(error)
              })
        })
      },
      //显示新增界面
      handleAdd(){
        this.saveFormVisible = true;
        this.getManager();
        this.saveForm ={
            username: '',
            manager:{
              id:null,
              username:""
            },
            state:null,
            shop:{
              id:null,
              name:""
            }
        }
      },
      //显示修改界面
      handleEdit: function (index, row) {
        this.getManager();
        this.saveFormVisible = true;
        this.saveForm = row

      },
      //编辑
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              //NProgress.start();
              this.$http.put('/employee', this.saveForm)
                  .then(res => res.data)
                  .then(res => {
                    if (res.success) {
                      this.saveLoading = false;
                      this.saveFormVisible = false;
                      this.handleCurrentChange(1);
                      this.$message({
                        message: "提交成功！",
                        type: 'success'
                      });
                    } else {
                      this.$message({
                        message: res.message,
                        type: 'error'
                      });
                    }
                  })
                  .catch(error => {
                    console.log(error)
                  })
            });
          }
        });
      },
      selsChange: function (sels) {
        this.sels = sels;
      },
      //批量删除
      batchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          this.$http.patch('/employee',ids)
              .then(res => res.data)
              .then(res => {
                if (res.success) {
                  this.listLoading = false;
                  this.handleCurrentChange(1);
                  this.$message({
                    message: "删除成功！",
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: res.message,
                    type: 'error'
                  });
                }
              })
              .catch(error => {
                console.log(error)
              })
        })
      },
      deptTree(){
        this.$http.get('/employee/tree')
            .then(res => res.data)
            .then(res => {
              if (res.success) {
                this.deptTree = res.data;
              } else {
                this.$message({
                  message: res.message,
                  type: 'error'
                });
              }
            })
            .catch(error => {
              console.log(error)
            })
      }
    },
		mounted() {
			this.getEmployees();
		}
	}
</script>

<style scoped>

</style>